package PokeGame;

import java.util.*;

/**
 * @Auther: MFM
 * @Date: 2021/7/29 - 07 - 29 - 14:34
 * @version: 1.0
 */

public class pokeGame {
    public static void startGame() {
        createPoke();
    }

    //创建牌组
    public static void createPoke() {
        String[] color = {"红桃", "黑桃", "梅花", "方片"};
        String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        List<String> pokeList = new ArrayList<>();
        for (int i = 0; i < color.length; i++) {
            for (int j = 0; j < num.length; j++) {
                pokeList.add(color[i] + num[j]);
            }
        }
        //创建完毕 开始洗牌
        shuffle(pokeList);
    }

    //洗牌操作 需要牌组
    public static void shuffle(List<String> list) {
//        Random random = new Random();
//        for (int b = 0; b < 100; b++) {
//            int i = random.nextInt(list.size());
//            String a = list.get(i);
//            list.set(i,list.get(b));
//            list.set(b,a);
//        }
        Collections.shuffle(list);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入游玩人数");
        int i = scanner.nextInt();
        scanner.close();
        //洗牌完毕 开始发牌
        licensing(i, list);
    }

    //发牌 需要玩家人数以及牌组
    private static void licensing(int playerNum, List<String> list) {
        if (playerNum >= 2 && playerNum <= 5) {
            Map<String, TreeSet> map = new HashMap<>();
            //添加玩家
            for (int i = 0; i < playerNum; i++) {
                map.put("玩家" + (i + 1), new TreeSet<String>((str1, str2) -> {
                    //花色
                    String[] split1 = str1.split("\\w");
                    String[] split2 = str2.split("\\w");
                    //点数
                    String s1 = str1.substring(2);
                    String s2 = str2.substring(2);
                    String basicChar = "2345678910JQKA";
                    String Color = "红桃黑桃梅花方片";
                    if (basicChar.indexOf(s1) > basicChar.indexOf(s2)) {
                        return -1;
                    } else if (basicChar.indexOf(s1) < basicChar.indexOf(s2)) {
                        return 1;
                    } else {
                        if (Color.indexOf(split1[0]) > Color.indexOf(split2[0])) {
                            return 1;
                        } else if (Color.indexOf(split1[0]) < Color.indexOf(split2[0])) {
                            return -1;
                        } else {
                            return 0;
                        }
                    }
                }));
            }

            Random random = new Random();
            int index = -1;

            //每轮发一张牌，发五轮
            for (int i = 0; i < 5; i++) {
                //几个人发几次
                for (int j = 0; j < playerNum; j++) {
                    int r = random.nextInt(list.size());
                    map.get("玩家" + (j + 1)).add(list.remove(r));

                }
            }

            //发牌完毕 输出每个玩家的牌
            for (int i = 0; i < playerNum; i++) {
                System.out.println("玩家" + (i + 1) + ":" + map.get("玩家" + (i + 1)));
            }

            //评分
            Map<String, Double> source = new HashMap<>();
            for (int i = 0; i < playerNum; i++) {
                source.put("玩家" + (i + 1), createSource(map.get("玩家" + (i + 1))));
            }
            System.out.println(source);

            //输出获胜者
            double max = 0;
            String player = "";
            for (int i = 0; i < source.size(); i++) {
                if (source.get("玩家" + (i + 1)) > max) {
                    max = source.get("玩家" + (i + 1));
                    player = "玩家" + (i + 1);
                }
            }

            System.out.println("本轮获胜者是：" + player);
            System.out.println(player + "的得分是" + max);
        } else {
            System.out.println("人数不符合规定，退出游戏");
            System.exit(0);
        }
    }

    //计算得分
    public static double createSource(TreeSet<String> set) {
        double source = 0;
        int[] st = new int[5];
        for (int i = 0; i < 5; i++) {
            String s = set.pollFirst();
            assert s != null;
            String s1 = s.substring(2);

            if (s1.equals("J")) {
                s1 = "11";
            } else if (s1.equals("Q")) {
                s1 = "12";
            } else if (s1.equals("K")) {
                s1 = "13";
            } else if (s1.equals("A")) {
                s1 = "14";
            }

            int substring = Integer.parseInt(s1);
            st[i] = substring;
        }

        if (st[0] == st[1] + 1 && st[1] == st[2] + 1 && st[2] == st[3] + 1 && st[3] == st[4] + 1) {
            source += 5;
            source += (st[0] * 0.01);
        } else if (st[0] == st[3] || st[1] == st[4]) {
            source += 4;
            source += (st[2] * 0.01);
        } else if (st[0] == st[2] || st[1] == st[3] || st[2] == st[4]) {
            source += 3;
            source += (st[2] * 0.01);
        } else if (st[0] == st[1] || st[1] == st[2] || st[2] == st[3] || st[3] == st[4]) {
            source += 2;
            int a = 0;
            for (int i = 0; i < 4; i++) {
                if (st[i] == st[i + 1]) {
                    a++;
                }
            }

            if (a == 2) {
                int d = 0;
                for (int i = 0; i < 4; i++) {
                    if (st[i] == st[i + 1]) {
                        source += (st[i] * 0.01);
                        d = i;
                        break;
                    }
                }
                for (int i = d + 1; i < 4; i++) {
                    if (st[i] == st[i + 1]) {
                        source += (st[i] * 0.0001);
                    }
                }
            } else {
                for (int i = 0; i < 4; i++) {
                    if (st[i] == st[i + 1]) {
                        source += (st[i] * 0.0001);
                    }
                }
            }
        } else {
            source = source + 1;
            source += (st[0] * 0.01);
        }

        return source;
    }

    public static void main(String[] args) {
        startGame();
    }

}
